﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace WCSY.DAL
{
    public class wcsy_costsOtherOrderDetService
    {
        public wcsy_costsOtherOrderDetService()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DBHelper.GetMaxID("lineid", "wcsy_costsOtherOrderDet"); 
		}


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string sysno,int lineid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from wcsy_costsOtherOrderDet");
			strSql.Append(" where sysno='"+sysno+"' and lineid="+lineid+" ");
			return DBHelper.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public string Add(WCSY.MODEL.wcsy_costsOtherOrderDet model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.sysno != null)
			{
				strSql1.Append("sysno,");
				strSql2.Append("'"+model.sysno+"',");
			}
			if (model.lineid != null)
			{
				strSql1.Append("lineid,");
				strSql2.Append(""+model.lineid+",");
			}
			if (model.costsid != null)
			{
				strSql1.Append("costsid,");
				strSql2.Append("'"+model.costsid+"',");
			}
			if (model.discrip != null)
			{
				strSql1.Append("discrip,");
				strSql2.Append("'"+model.discrip+"',");
			}
			if (model.applyMoney != null)
			{
				strSql1.Append("applyMoney,");
				strSql2.Append(""+model.applyMoney+",");
			}
			if (model.projNo != null)
			{
				strSql1.Append("projNo,");
				strSql2.Append("'"+model.projNo+"',");
			}
			if (model.remarks != null)
			{
				strSql1.Append("remarks,");
				strSql2.Append("'"+model.remarks+"',");
			}
			strSql.Append("insert into wcsy_costsOtherOrderDet(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			return strSql.ToString();
			
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(WCSY.MODEL.wcsy_costsOtherOrderDet model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update wcsy_costsOtherOrderDet set ");
			if (model.costsid != null)
			{
				strSql.Append("costsid='"+model.costsid+"',");
			}
			else
			{
				strSql.Append("costsid= null ,");
			}
			if (model.discrip != null)
			{
				strSql.Append("discrip='"+model.discrip+"',");
			}
			else
			{
				strSql.Append("discrip= null ,");
			}
			if (model.applyMoney != null)
			{
				strSql.Append("applyMoney="+model.applyMoney+",");
			}
			else
			{
				strSql.Append("applyMoney= null ,");
			}
			if (model.projNo != null)
			{
				strSql.Append("projNo='"+model.projNo+"',");
			}
			else
			{
				strSql.Append("projNo= null ,");
			}
			if (model.remarks != null)
			{
				strSql.Append("remarks='"+model.remarks+"',");
			}
			else
			{
				strSql.Append("remarks= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where sysno='"+ model.sysno+"' and lineid="+ model.lineid+" ");
			int rowsAffected=DBHelper.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string sysno,int lineid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from wcsy_costsOtherOrderDet ");
			strSql.Append(" where sysno='"+sysno+"' and lineid="+lineid+" " );
			int rowsAffected=DBHelper.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
        /// <summary>
        /// 根据sysno删除该单据所有数据
        /// </summary>
        public string Delete(string sysno)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from wcsy_costsOtherOrderDet ");
            strSql.Append(" where sysno='" + sysno + "' ");
            return strSql.ToString();
            
        }
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public WCSY.MODEL.wcsy_costsOtherOrderDet GetModel(string sysno,int lineid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" sysno,lineid,costsid,discrip,applyMoney,projNo,remarks ");
			strSql.Append(" from wcsy_costsOtherOrderDet ");
			strSql.Append(" where sysno='"+sysno+"' and lineid="+lineid+" " );
			WCSY.MODEL.wcsy_costsOtherOrderDet model=new WCSY.MODEL.wcsy_costsOtherOrderDet();
			DataSet ds=DBHelper.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["sysno"]!=null && ds.Tables[0].Rows[0]["sysno"].ToString()!="")
				{
					model.sysno=ds.Tables[0].Rows[0]["sysno"].ToString();
				}
				if(ds.Tables[0].Rows[0]["lineid"]!=null && ds.Tables[0].Rows[0]["lineid"].ToString()!="")
				{
					model.lineid=int.Parse(ds.Tables[0].Rows[0]["lineid"].ToString());
				}
				if(ds.Tables[0].Rows[0]["costsid"]!=null && ds.Tables[0].Rows[0]["costsid"].ToString()!="")
				{
					model.costsid=ds.Tables[0].Rows[0]["costsid"].ToString();
				}
				if(ds.Tables[0].Rows[0]["discrip"]!=null && ds.Tables[0].Rows[0]["discrip"].ToString()!="")
				{
					model.discrip=ds.Tables[0].Rows[0]["discrip"].ToString();
				}
				if(ds.Tables[0].Rows[0]["applyMoney"]!=null && ds.Tables[0].Rows[0]["applyMoney"].ToString()!="")
				{
					model.applyMoney=decimal.Parse(ds.Tables[0].Rows[0]["applyMoney"].ToString());
				}
				if(ds.Tables[0].Rows[0]["projNo"]!=null && ds.Tables[0].Rows[0]["projNo"].ToString()!="")
				{
					model.projNo=ds.Tables[0].Rows[0]["projNo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["remarks"]!=null && ds.Tables[0].Rows[0]["remarks"].ToString()!="")
				{
					model.remarks=ds.Tables[0].Rows[0]["remarks"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select sysno,lineid,costsid,discrip,applyMoney,projNo,remarks ");
			strSql.Append(" FROM wcsy_costsOtherOrderDet ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DBHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" sysno,lineid,costsid,discrip,applyMoney,projNo,remarks ");
			strSql.Append(" FROM wcsy_costsOtherOrderDet ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DBHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM wcsy_costsOtherOrderDet ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DBHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.lineid desc");
			}
			strSql.Append(")AS Row, T.*  from wcsy_costsOtherOrderDet T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DBHelper.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
    }
}
